package com.mystore.service.impl;

import com.mystore.dao.GoodDao;
import com.mystore.dao.impl.GoodDaoImpl;
import com.mystore.entity.Good;
import com.mystore.service.GoodService;
import com.mystore.util.DBUtil;
import com.mystore.util.PageUtil;
import com.mystore.vo.GoodVO;

import java.sql.Connection;
import java.util.List;

public class GoodServiceImpl implements GoodService {

    @Override
    public Good findByid(int id) {
        Connection connection = DBUtil.getConntion();
        GoodDao goodDao = new GoodDaoImpl();
        Good good = goodDao.finById(id,connection);
        DBUtil.closeConnection(connection);
        return good;
    }

    /**
     * 查询所有的商品信息
     * @return
     */
    @Override
    public synchronized List<GoodVO> finAllGood() {
        Connection connection = DBUtil.getConntion();
        GoodDao goodDao = new GoodDaoImpl();
        List<GoodVO> list = goodDao.finAllGood(connection);
        DBUtil.closeConnection(connection);
        return list;
    }

    /**
     * 查询所有的符合条件记录数并分类
     * @param gid
     * @param pageSize
     * @param pageNumber
     * @return
     */

    @Override
    public PageUtil<GoodVO> findAllGoodByGid(int gid, int pageSize, int pageNumber) {
        Connection connection = DBUtil.getConntion();
        GoodDao goodDao = new GoodDaoImpl();
        PageUtil<GoodVO> pageUtil = new PageUtil<>();
        pageUtil.setPageNumber(pageNumber);
        pageUtil.setPageSize(pageSize);
        pageUtil.setPageTotal(goodDao.getTotal(gid,connection));
        pageUtil.setList(goodDao.findAllGoodByGid(gid,pageSize,pageNumber,connection));
        DBUtil.closeConnection(connection);
        return pageUtil;
    }
}
